<?php
defined('IN_TS') or die('Access Denied.');
$userid = aac('user')->isLogin();
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;

$lstart = $page*30-30;

$groupid = trim($_GET['groupid']);

$supporter = trim($_GET['supporter']);
//$orgid = trim($_GET['orgid']);
$begintime = trim($_GET['begintime']);
$endtime = trim($_GET['endtime']);
$all =  trim($_GET['all']);


if($endtime==''){
	$endtime=date('Y-m-d',time());
}


if($begintime==''){
	$begintime=date('Y-m-d',strtotime('-2 week'));
}


$url = tsUrl ( 'group', 'vo', array ('all'=>$all,'groupid'=>$groupid,'begintime'=>$begintime,'endtime'=>$endtime,'supporter'=>$supporter,'page' => '') );
//$strOrg = $this->findAll('user_org');

$title = '';



switch($all){
	case 1:
		$str='isstock=0 and isnull(count_duty) and isvo<>0 and not isnull(supporter)';
		break;
	case 2:
		$str='isvo=1 and isnull(count_duty) and wechat=0 and isstock=0  and not isnull(supporter)';
		break;
	case 3:
		$str='isvo=1 and isnull(count_duty) and wechat=0 and isstock=0 and isnull(supporter)';
		break;
	default:
		$str='isvo=1 and isnull(count_duty) and wechat=0 and isstock=0';
}


if($supporter!=''){
	$str = $str.' and supporter='.$supporter;
	$user = aac('user')->find('user_info',array('userid'=>$supporter));
	$title = $user['username'].'打捞的';
}

if($groupid!=''){
	$str = $str.' and groupid='.$groupid;
	$group = aac('group')->getOneGroup($groupid);
	if(!in_array($userid,$group['admin'])&&$TS_USER ['isadmin'] == 0){
		tsNotice('你没有操作权限！');
	}

	$title = $title.$group['groupname'];
}else{
	$str = $str.' and groupid in(select groupid from ts_group where isplace=2)';
	if($TS_USER ['isadmin'] == 0){
		tsNotice('你没有操作权限！');
	}
}

/*
if($orgid!=''){
	$arrUser= aac('user')->findAll('user_info',array('orgid'=>$orgid),null,'userid');

	$uids='';
	foreach ($arrUser as $v) {
		if($uids==''){
			$uids = $v['userid'];
		}else{
			$uids=$uids.','.$v['userid'];
		}
	}
	$str = $str.' and userid in ('.$uids.")";
	$org = $this->find('user_org',array('orgid'=>$orgid));
	$title = $title.$org['orgname'];
}
*/

if($begintime!=''&& $endtime!=''){

	$str =$str.' and addtime>'.strtotime($begintime).' and addtime<'.strtotime($endtime);
	$title = $title.date('ymd',strtotime($begintime)).'-'.date('ymd',strtotime($endtime));
}

$strStock = aac('group')->findAll('group_user',$str,'addtime desc',null, $lstart . ',30' );

foreach($strStock as $k=>$v){
	$user = aac('user')->getOneUser($v['userid']);
	$strStock[$k]['user'] = $user;
	$memo = aac('user')->find('user_comment',array('userid'=>$v['userid'],'type'=>1),'','addtime desc');
	if($memo){
		$menoauthor = aac('user')->find('user_info',array('userid'=>$memo['authorid']));
		$memo['author'] = $menoauthor['username'];
		$strStock[$k]['memo'] = $memo;
	}
	$strSupporter = aac('user')->find('user_info',array('userid'=>$v['supporter']));
	$strStock[$k]['supportername'] = $strSupporter['username'];
	$strStock[$k]['phone'] = $user['phone'];
	$arrgroup=aac('group')->find('group',array('groupid'=>$v['groupid']));
	$strStock[$k]['group'] = $arrgroup;
	if (!in_array($v['groupid'],$groupids)){
		$groupids[]= $v['groupid'];
	}
	if($supporter){
		if (!in_array($v['supporter'],$supporters)){
			$supporters[]= $v['supporter'];
		}
	}
}
$groupids = array();
$supporters = array();
$allStock =  aac('group')->findAll('group_user','isvo=1 and wechat=0 and isstock=0' );
foreach($allStock as $k=>$v){
	if (!in_array($v['groupid'],$groupids)){
		$groupids[]= $v['groupid'];
	}
	if($v['supporter']){
		if (!in_array($v['supporter'],$supporters)){
			$supporters[]= $v['supporter'];
		}
	}
}

$groupids = implode(',',$groupids);
$supporters = implode(',',$supporters);
$strGroup = aac('group')->findAll('group','isplace=2 and groupid in ('.$groupids.')');
$strSupporter = aac('user')->findAll('user_info','userid in ('.$supporters.')');
$stockNum = aac('group')->findCount('group_user',$str);
$pageUrl = pagination ( $stockNum, 30, $page, $url );

$title = $title.'馆员打捞名单';

include template("vo");